package bartenderqueue;

import drinkkiosk.drinks.SpecialDrink;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import networktool.BartenderClient;
import networktool.BartenderServer;

/** BartenderQueue
 * This program provides a Bartender and Wait Staff at a private bar to 
 * receive drink orders from multiple DrinkKiosk clients servicing a range
 * of Patrons. Patrons select their drinks at a DrinkKiosk and the client
 * sends a Drink to the server which enters the DrinkQueue and is displayed
 * as text inside a large DrinkButton. When a Drink is prepared by the Bartender,
 * they will touch the DrinkButton which changes the background color. This alerts
 * available Wait Staff that the Drink is ready to be served. Once a Wait Staff
 * picks up the Drink, they will press the DrinkButton again and the DrinkQueue
 * will remove the Drink.
 *
 * @author Charles Ritchea
 */
public class BartenderQueue extends javax.swing.JFrame {
    static private int port;
    static private Socket server;
    static private ServerSocket listener;
    /** Creates new form BartenderQueue */
    public BartenderQueue() {
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        DrinkPanel = new javax.swing.JPanel();
        drinkButton1 = new bartenderqueue.DrinkButton();
        drinkButton2 = new bartenderqueue.DrinkButton();
        drinkButton3 = new bartenderqueue.DrinkButton();
        drinkButton4 = new bartenderqueue.DrinkButton();
        drinkButton5 = new bartenderqueue.DrinkButton();
        drinkButton6 = new bartenderqueue.DrinkButton();
        drinkButton7 = new bartenderqueue.DrinkButton();
        drinkButton8 = new bartenderqueue.DrinkButton();
        TitlePanel = new javax.swing.JPanel();
        exitButton1 = new bartenderqueue.ExitButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Interactive Solutions' Bartender Queue");
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        setUndecorated(true);

        DrinkPanel.setLayout(new java.awt.GridLayout(2, 2));
        DrinkPanel.add(drinkButton1);
        DrinkPanel.add(drinkButton2);
        DrinkPanel.add(drinkButton3);
        DrinkPanel.add(drinkButton4);
        DrinkPanel.add(drinkButton5);
        DrinkPanel.add(drinkButton6);
        DrinkPanel.add(drinkButton7);
        DrinkPanel.add(drinkButton8);

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        exitButton1.setContentAreaFilled(false);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 24));
        jLabel1.setText("Interactive Solutions' Bartender Queue");

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 334, Short.MAX_VALUE)
                .addComponent(exitButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exitButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(DrinkPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 820, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(DrinkPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 530, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Settings.loadSettings();
        port = Integer.parseInt(Settings.getBartenderServerPort());
        final BartenderQueue bartenderQueue = new BartenderQueue();
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BartenderQueue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BartenderQueue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BartenderQueue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BartenderQueue.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            /** run
             * 
             */
            @Override
            public void run() {
        bartenderQueue.initComponents();
                bartenderQueue.setAlwaysOnTop(true);
                bartenderQueue.setVisible(true);
                goFullScreen();
                try {
                    BartenderClient.sendDrink(new SpecialDrink("online","","","","","","",""));
                } catch (IOException ex) {
                    Logger.getLogger(BartenderQueue.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
		try {
			listener = new ServerSocket(port);
			while(true){
				server = listener.accept();
				BartenderServer bartenderServer = new BartenderServer(server, bartenderQueue.DrinkPanel);
				Thread t = new Thread(bartenderServer);
				t.start();
			}
		} catch (IOException ex) {
			Logger.getLogger(BartenderServer.class.getName()).log(Level.SEVERE, null, ex);
		}
    }
    private static void goFullScreen() {
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        if (graphicsDevices[0].isFullScreenSupported()) {
            graphicsDevices[0].setFullScreenWindow(getWindows()[0]);
            DisplayMode[] dms = graphicsDevices[0].getDisplayModes();
            graphicsDevices[0].setDisplayMode(new DisplayMode(800, 600, 32, 60));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel DrinkPanel;
    private javax.swing.JPanel TitlePanel;
    private bartenderqueue.DrinkButton drinkButton1;
    private bartenderqueue.DrinkButton drinkButton2;
    private bartenderqueue.DrinkButton drinkButton3;
    private bartenderqueue.DrinkButton drinkButton4;
    private bartenderqueue.DrinkButton drinkButton5;
    private bartenderqueue.DrinkButton drinkButton6;
    private bartenderqueue.DrinkButton drinkButton7;
    private bartenderqueue.DrinkButton drinkButton8;
    private bartenderqueue.ExitButton exitButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}